package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.dialect.helper.MySqlDialect;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Meltedicecream
 * @since 2023/06/20 22:45
 */
public class AdminCouponServiceImpl implements AdminCouponService {
    @Override
    public List<MarketCoupon> list(Integer page, Short limit, String sort, String order, String name, String type, String status) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper mapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();


        //判断name与content是否为空，空则返回所有数据，非空则添加where语句
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            Short shortType = Short.parseShort(type);
            criteria.andTypeEqualTo(shortType);
        }
        if (!StringUtils.isEmpty(status)) {
            Short shortStatus = Short.parseShort(status);
            criteria.andStatusEqualTo(shortStatus);
        }
        // 在sql语句中加入排序规则
        marketCouponExample.setOrderByClause(sort + " " + order);
        //使用分页插件
        PageHelper.startPage(page, limit);
        //返回广告集合
        List<MarketCoupon> coupons = mapper.selectByExample(marketCouponExample);
        session.commit();
        session.close();
        return coupons;
    }

    @Override
    public MarketCoupon update(Integer id, String name, String desc, String tag, Integer total, BigDecimal discount, BigDecimal min, Short limit, Short type, Short status, Short goodsType, Integer[] goodsValue, Short timeType, Short days, LocalDateTime addTime, LocalDateTime updateTime) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = new MarketCoupon();
        //给该对象赋值
        marketCoupon.setId(id);
        marketCoupon.setName(name);
        marketCoupon.setDesc(desc);
        marketCoupon.setTag(tag);
        marketCoupon.setTotal(total);
        marketCoupon.setDiscount(discount);
        marketCoupon.setMin(min);
        marketCoupon.setLimit(limit);
        marketCoupon.setType(type);
        marketCoupon.setStatus(status);
        marketCoupon.setGoodsType(goodsType);
        marketCoupon.setGoodsValue(goodsValue);
        marketCoupon.setTimeType(timeType);
        marketCoupon.setDays(days);
        marketCoupon.setAddTime(addTime);
        marketCoupon.setUpdateTime(updateTime);
        //操作数据库修改数据
        marketCouponMapper.updateByPrimaryKey(marketCoupon);
        session.commit();
        session.close();
        //返回
        return marketCoupon;

    }

    @Override
    public int delete(Integer id) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        int i = marketCouponMapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
        return i;
    }

    @Override
    public MarketCoupon create(String name, String desc, String tag, Integer total, BigDecimal discount, BigDecimal min, Short limit, Short type, Short status, Short goodsType, Integer[] goodsValue, Short timeType, Short days, LocalDateTime startTime, LocalDateTime endTime) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setName(name);
        marketCoupon.setDesc(desc);
        marketCoupon.setTag(tag);
        marketCoupon.setTotal(total);
        marketCoupon.setDiscount(discount);
        marketCoupon.setMin(min);
        marketCoupon.setLimit(limit);
        marketCoupon.setType(type);
        marketCoupon.setStatus(status);
        marketCoupon.setGoodsType(goodsType);
        marketCoupon.setGoodsValue(goodsValue);
        marketCoupon.setTimeType(timeType);
        marketCoupon.setDays(days);
        marketCoupon.setStartTime(startTime);
        marketCoupon.setEndTime(endTime);
        LocalDateTime now = LocalDateTime.now();
        marketCoupon.setAddTime(now);
        marketCoupon.setUpdateTime(now);
        marketCouponMapper.insert(marketCoupon);
        session.commit();
        session.close();
        return marketCoupon;
    }

    @Override
    public List<MarketCoupon> read(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        criteria.andIdEqualTo(Integer.parseInt(id));
        List<MarketCoupon> marketCouponList = marketCouponMapper.selectByExample(marketCouponExample);
        return marketCouponList;
    }

    @Override
    public List<MarketCouponUser> listuser(Integer page, Integer limit, Integer couponId, String sort, String order) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponUserMapper marketCouponUserMapper = session.getMapper(MarketCouponUserMapper.class);
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        //设置排序规则
        marketCouponUserExample.setOrderByClause(sort + " " + order);
        //查询couponId
        criteria.andCouponIdEqualTo(couponId);
        //使用分页插件
        PageHelper.startPage(page,limit);
        //查询返回List
        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        session.commit();
        session.close();
        return marketCouponUserList;
    }
}
